package com.sxyjhh.dbutil.basic;


import com.sxyjhh.dbutil.utils.DBQueryThreadPool;
import lombok.extern.log4j.Log4j;

import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Description: 任务管理器
 *
 * @JDK Version:1.8.0_40
 * Version: V1.0
 * Author: wangchun
 * Date: 2016/3/24 10:41
 */
@Log4j
public class ClientWorkManager {

    private static ClientWorkManager instance;

    private static Map<String, ClientWork> taskMap = new HashMap<String, ClientWork>();

    private ClientWorkManager() {
    }

    public static ClientWorkManager getInstance() {
        if (instance == null) {
            instance = new ClientWorkManager();
        }
        return instance;
    }

    /**
     * 创建一个ClientWork
     *
     * @param workParams         work参数
     * @param clientWorkCallback work回调
     * @return work id
     */
    public String createClientWork(FrontEndVo workParams, ClientWorkCallback clientWorkCallback) {
        ClientWork clientWork = new ClientWork(workParams, clientWorkCallback);
        String workId = clientWork.getWorkId();
        taskMap.put(workId,clientWork);
        log.info("createClientWork workid= " + workId);
        log.info("createClientWork work count= " + taskMap.keySet().size());
        return workId;
    }

    /**
     * 根据work id获取work参数
     *
     * @param workId
     * @return
     */
    public Object getWorkParamsByWorkId(String workId) {
        if (taskMap.containsKey(workId)) {
            taskMap.get(workId);
        }
        return null;
    }

    /**
     * 停止所有的工作
     *
     * @return
     */
    public boolean stopAllWork() {
        Collection<ClientWork> works = taskMap.values();
        //停止所有的工作
        for (ClientWork work:works){
            work.stopWork();
        }
        taskMap.clear();
        return true;
    }

    /**
     * 停止某一项工作
     *
     * @param workId 工作id
     * @return
     */
    public boolean stopWorkByWorkId(String workId) {
        if (taskMap.containsKey(workId)) {
            ClientWork clientWork = taskMap.get(workId);
            clientWork.stopWork();
            taskMap.remove(workId);

            DBQueryThreadPool.getInstance().shutdown(workId);
        }
        return true;
    }

    /**
     * 将任务加入的执行列表,加入延时执行
     *
     * @param task  任务
     * @param delay 延迟时间毫秒，0为不延迟
     */
    public void schedule(String workId, ClientTask task, long delay) {
        if (taskMap.containsKey(workId)) {
            ClientWork clientWork = taskMap.get(workId);
            clientWork.schedule(task, delay);
        }
    }

    /**
     * 将任务加入的执行列表,按指定的日期时间执行
     *
     * @param task 任务
     * @param time 制定时间点
     */
    public void schedule(String workId, ClientTask task, Date time) {
        if (taskMap.containsKey(workId)) {
            ClientWork clientWork = taskMap.get(workId);
            clientWork.schedule(task, time);
        }
    }

    /**
     * 重复执行任务，按固定的任务间隔，但频率可能会有一些延时
     *
     * @param task   任务
     * @param delay  延迟时间毫秒，0为不延迟
     * @param period 重复执行时间间隔，时间间隔单位毫秒
     */
    public void schedule(String workId, ClientTask task, long delay, long period) {
        if (taskMap.containsKey(workId)) {
            ClientWork clientWork = taskMap.get(workId);
            clientWork.schedule(task, delay, period);
        }
    }

    /**
     * 以固定的时间作为起点，重复执行任务，按固定的任务间隔，但频率可能会有一些延时
     *
     * @param task      任务
     * @param firstTime 开始执行任务的时间
     * @param period    重复执行时间间隔，时间间隔单位毫秒
     */
    public void schedule(String workId, ClientTask task, Date firstTime, long period) {
        if (taskMap.containsKey(workId)) {
            ClientWork clientWork = taskMap.get(workId);
            clientWork.schedule(task, firstTime, period);
        }
    }

    /**
     * 重复执行任务，按固定的任务间隔，每次执行任务的频率一致
     *
     * @param task   任务
     * @param delay  延迟时间毫秒，0为不延迟
     * @param period 重复执行时间间隔，时间间隔单位毫秒
     */
    public void scheduleAtFixedRate(String workId, ClientTask task, long delay, long period) {
        if (taskMap.containsKey(workId)) {
            ClientWork clientWork = taskMap.get(workId);
            clientWork.scheduleAtFixedRate(task, delay, period);
        }
    }

    /**
     * 以固定的时间作为起点，重复执行任务，按固定的任务间隔，每次执行任务的频率一致
     *
     * @param task      任务
     * @param firstTime 开始执行任务的时间
     * @param period    重复执行时间间隔，时间间隔单位毫秒
     */
    public void scheduleAtFixedRate(String workId, ClientTask task, Date firstTime, long period) {
        if (taskMap.containsKey(workId)) {
            ClientWork clientWork = taskMap.get(workId);
            clientWork.scheduleAtFixedRate(task, firstTime, period);
        }
    }

}
